#include "Common.h"
#include "Master.h"

DLL_EXPORT ConfigFile* world_config;
DLL_EXPORT Database* char_database;

void StartDb();

ConsoleParser<Listener>* ConParse;

int main()
{
	try{
#ifdef WIN32
	SetConsoleTitle("crCRcr");
#endif
	printf(" @@@@@@@  @@@@@@@    @@@@@@@  @@@@@@@    @@@@@@@  @@@@@@@ \n");
	printf("@@@@@@@@  @@@@@@@@  @@@@@@@@  @@@@@@@@  @@@@@@@@  @@@@@@@@\n");
	printf("!@@       @@!  @@@  !@@       @@!  @@@  !@@       @@!  @@@\n");
	printf("!@!       !@!  @!@  !@!       !@!  @!@  !@!       !@!  @!@\n");
	printf("!@!       @!@!!@!   !@!       @!@!!@!   !@!       @!@!!@!\n");
	printf("!!!       !!@!@!    !!!       !!@!@!    !!!       !!@!@!\n");
	printf(":!!       !!: :!!   :!!       !!: :!!   :!!       !!: :!!\n");
	printf(":!:       :!:  !:!  :!:       :!:  !:!  :!:       :!:  !:!\n");
	printf(" ::: :::  ::   :::   ::: :::  ::   :::   ::: :::  ::   :::\n");
	printf(" :: :: :   :   : :   :: :: :   :   : :   :: :: :   :   : : \n\n\n");
	DWORD start = GetTickCount();
	new CLog();
	new World();
	world_config = new ConfigFile("world.txt");
	if(!world_config->Load())
	{
		getchar();
		return 0;
	}
	else
	{
		Log->Success("Config", "Loaded worldconfig!");
	}
	StartDb();
	sWorld.Init();
	new LogonClientHandler();
	new WorldSocket();
	ThreadPool.ExecuteTask(LogonClientHandler::getSingle());
	ThreadPool.ExecuteTask(WorldSocket::getSingle());
	Log->Success("Master", "Startuptime: %05.02f seconds", (GetTickCount() - start) / 1000.0f);
	Listener* listen = new Listener;
	ConParse = new ConsoleParser<Listener>(listen);
	ConParse->AddHandler("help", &Listener::ShowHelp);
	ConParse->AddHandler("announce", &Listener::Announce);
	ScopedAllocator<char> inp(255);
	size_t pos = 0;
	while(1)
	{
		if(_kbhit())
		{
			char c = _getch();
			switch(c)
			{
			case '\b':
				inp[--pos] = 0;
				_putch('\b');
				_putch(' ');
				_putch('\b');
				break;
			case '\r':
				inp[pos] = 0;
				_putch('\n');
				ConParse->ParseLine(inp);
				inp[pos = 0] = 0;
				break;
			default:
				inp[pos++] = c;
				_putch(c);
				break;
			}
		}
		Sleep(50);
	}
	ThreadPool.ShutDown();
	}
	catch(const char* ex)
	{
		Log->Notice("Master", "Runtime error: '%s'", ex);
#ifdef WIN32
		MessageBox(NULL, ex, "Runtime error", MB_OK);
#else
		getchar();
#endif
	}
}

void StartDb()
{
	char_database = new Database();
	char_database->Connect(world_config->GetChar("WorldDatabase", "Host"), world_config->GetChar("WorldDatabase", "User"), world_config->GetChar("WorldDatabase", "Pass"), world_config->GetChar("WorldDatabase", "Database"), 3306);
}

void Listener::ShowHelp(char* args)
{
	Log->Notice("Console", "Available commands:\n"\
		"help - Shows this help!\n"\
		"announce - Displays the given message to all online players.\n");
}

void Listener::Announce(char* args)
{
	if(!args)
	{
		Log->Notice("Console", "You need to give some text to send!");
		return;
	}
	sWorld.SendWorldText(args);
}